package models.misc
{
	import com.asfusion.mate.events.Dispatcher;
	
	import events.misc.LoginEvent;
	
	import flash.events.EventDispatcher;
	
	import models.Locale;
	import models.vos.accounts.ContactInfo;
	import models.vos.accounts.EmailAddress;
	import models.vos.accounts.LocalAddress;
	import models.vos.accounts.PhoneNumber;
	import models.vos.accounts.User;
	
	import mx.collections.ArrayCollection;
	
	public class UserManager extends EventDispatcher
	{
		private var dp:Dispatcher = new Dispatcher();
		
		[Bindable]
		public var loggedInUser:User;	
		
		
		public function UserManager(){
			super();
		}	
		
		public function requestRegister(evt:LoginEvent):void{

			if( evt.username == 'huydung' ){
				dp.dispatchEvent( new LoginEvent( LoginEvent.USERNAME_EXIST ));
				return;
			};
			if( evt.email == "vannessars@yahoo.com" ){
				dp.dispatchEvent( new LoginEvent( LoginEvent.EMAIL_EXIST ));
				return;
			};
			loggedInUser = new User(evt.username, evt.password, evt.email, 2);
		}
		
		public function requestLogin(evt:LoginEvent):void{
			var levt:LoginEvent ;
			if( evt.username == "huydung" && evt.password == "hienham" ){
				loggedInUser = UserManager.fakeLoggedinUser();
			
				dp.dispatchEvent( new LoginEvent( LoginEvent.SUCCESS ) );
			}else if( evt.username != "huydung"){
				levt= new LoginEvent( LoginEvent.USERNAME_NOT_EXIST );
				levt.errorMessage = Locale.USERNAME_NOT_EXIST;
				dp.dispatchEvent( levt );
			}else{
				levt = new LoginEvent( LoginEvent.PASS_NOT_MATCH );
				levt.errorMessage = Locale.PASS_NOT_MATCH;
				dp.dispatchEvent( levt );
			}
		}
		
		public function checkEmailExist(evt:LoginEvent):void{
			if( evt.email == 'vannessars@yahoo.com' ){
				var event:LoginEvent = new LoginEvent( LoginEvent.EMAIL_EXIST, evt.username, evt.password, evt.email );
				event.fullName = 'Nguyễn Huy Dũng A';
				event.shortName = "DũngNH";
				dp.dispatchEvent( event );
			} else {
				dp.dispatchEvent( new LoginEvent(LoginEvent.EMAIL_NOT_EXIST, evt.username, evt.password, evt.email ));
			}	
		}

		//FAKE THE SERVICE
		public static function fakeLoggedinUser():User{
			var user:User = new User();
			user.username = "huydung";
			user.password = "hienham";
			user.active = true;
			user.confirmed = true;
			user.id = 1;
			user.registeredDate = new Date(2009, 5, 14, 20, 15, 35);
			
			var emails:ArrayCollection = new ArrayCollection();
			emails.addItem( new EmailAddress('contact.huydung@gmail.com', 'Freelance', true,  1) );
			emails.addItem( new EmailAddress('vannessars@yahoo.com', 'Personal', false,  2) );
			emails.addItem( new EmailAddress('nhdung@indochinapro.com', 'IndochinaPro', false,  3) );
			
			var phones:ArrayCollection = new ArrayCollection();
			phones.addItem( new PhoneNumber('0984903707', 'Mobile', true, 1) );
			phones.addItem( new PhoneNumber('8445187079', 'Home', false, 2) );
			
			var addresses:ArrayCollection = new ArrayCollection();
			addresses.addItem( new LocalAddress('17 Quán Thánh, Ba Đình, Hà Nội', 'Official', false, 1) );
			addresses.addItem( new LocalAddress('138/191 ngõ Văn Chương, Tôn Đức Thắng, Hà Nội', 'current', true, 1) );
						
			var cont:ContactInfo = new ContactInfo("Nguyễn Huy Dũng", "huydung", emails, phones, addresses, 1 );
			user.contactInfo = cont;
			user.contactCards = new ArrayCollection();
			
			return user;			
		}
	}
}